package com.example.textviewactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.textviewactivity.pojo.User;

import java.io.Serializable;

public class LogonActivity extends AppCompatActivity {

    CheckBox eat, dreak, play, happy;
    RadioGroup sex;
    RadioButton boy, girl;
    EditText ed_name, ed_pwd, ed_sure, ed_phone;
    Button logon;
    String gander="";
    String[] like=new String[4];
    private static final String TAG = "LogonActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logon);
        ed_name = findViewById(R.id.ed_name);
        ed_pwd = findViewById(R.id.ed_pwd);
        ed_sure = findViewById(R.id.ed_sure);
        logon = findViewById(R.id.logon);
        ed_phone = findViewById(R.id.ed_phone);
        eat = findViewById(R.id.eat);
        dreak = findViewById(R.id.dreak);
        play = findViewById(R.id.play);
        happy = findViewById(R.id.happy);
        sex = findViewById(R.id.sex);
        boy = findViewById(R.id.boy);
        girl = findViewById(R.id.girl);
    }

    public void logon(View view) {
        String name = "", pwd = "", sure = "", phone = "",sex="";
        name = ed_name.getText().toString();
        pwd = ed_pwd.getText().toString();
        sure = ed_sure.getText().toString();
        phone = ed_phone.getText().toString();
        Boolean flag = eat.isChecked() == true || dreak.isChecked() == true || play.isChecked() == true || happy.isChecked() == true ? true : false;
        Boolean hobby = boy.isChecked() == true || girl.isChecked() == true ? true : false;
        if ("".equals(name) || "".equals(pwd) || "".equals(sure) || "".equals(phone)) {
            Toast.makeText(this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if (name.length() <= 5) {
                Toast.makeText(this, "用户名长度必须大于5", Toast.LENGTH_SHORT).show();
                return;
            }
            if (pwd.length() <= 6) {
                Toast.makeText(this, "用户名长度必须大于6", Toast.LENGTH_SHORT).show();
                return;
            }
            if (phone.length() != 11) {
                Toast.makeText(this, "手机号码必须是11位", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!flag) {
                Toast.makeText(this, "兴趣不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!pwd.equals(sure)) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!hobby) {
                Toast.makeText(this, "性别不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (eat.isChecked()){
                like[0]=eat.getText().toString();
            }
            if (dreak.isChecked()){
                like[1]=dreak.getText().toString();
            }
            if (play.isChecked()){
                like[2]=play.getText().toString();
            }
            if (happy.isChecked()){
                like[3]=happy.getText().toString();
            }

            if (boy.isChecked()){
                gander="男";
            }else if (girl.isChecked()){
                gander="女";
            }
            User user=new User();
            user.setName(name);
            user.setPassword(pwd);
            user.setSex(gander);
            user.setHobby(like);
            user.setPhone(phone);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.putExtra("user",user);
            setResult(200,intent);
            Log.d(TAG, "logon: ===================="+gander);
            finish();
        }
    }

}
